-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
add new abstract type for iterators with HasShape: ShapefulIterator
#58052
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
| ndims(c::AbstractChar) = 0 | ||
| ndims(::Type{<:AbstractChar}) = 0 | ||
| length(c::AbstractChar) = 1 | ||
| IteratorSize(::Type{Char}) = HasShape{0}() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NB: here IteratorSize was defined for Char. With this PR it's defined for all AbstractChar subtypes (making this kinda a minor change). However that's OK, because this method should have been IteratorSize(::Type{AbstractChar}) = HasShape{0}() in the first place. Refer to:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ndims being defined for AbstractChar implies IteratorSize should be treated the same, I'd say.
5754426 to
c4e8f44
Compare
|
Notes:
|
|
An interesting consequence of this PR is that julia> typeof([3, [7]])
Vector{ShapefulIterator} (alias for Array{ShapefulIterator, 1}) |
Iterators with `HasShape{N}` are certainly the most well-behaved class
of iterators in Julia. Currently several separate type trees of such
iterators come with Julia. It seems like joining these trees under a
single abstract type would be nice.
Why this change is useful:
* Make expressing hardcoded `Union` types for zero-dimensional
collections unnecessary: instead of
`Union{Number,Ref,AbstractArray{<:Any,0}}` now it will be possible to
write simply, and more generically, `ShapefulIterator{0}`. At
least in some cases.
* Reduce the number of `ndims` and `IteratorSize` methods:
* The number of `ndims` methods that come with Julia is
decreased by six
* The number of `IteratorSize` methods that come with Julia is
decreased by three
Why this change doesn't prevent a better future supertype choice:
* Naively it might make sense to have `Number`, `AbstractChar` and
`Ref` subtype a parameterless abstract type like
`ZeroDimensionalIterator`. However:
* This seems less useful than `ShapefulIterator{N}`
* Adding `ShapefulIterator` doesn't preclude adding
`ZeroDimensionalIterator <: ShapefulIterator{0}` later.
* One might ask: why not make `AbstractArray{T}` subtype a different
abstract type, with an element type type parameter, something like
`AbstractArray{T,N} <: TypedIterator{T}`. In this case, however,
`TypedIterator` would be useless for the zero dimensional iterators
mentioned above, as they are their own element types, something not
expressible in abstract type subtyping.
c4e8f44 to
70c2516
Compare
|
Giving up on this, at least for now, as I have too many open PRs to handle. |
Iterators with
HasShape{N}are certainly the most well-behaved class of iterators in Julia. Currently several separate type trees of such iterators come with Julia. It seems like joining these trees under a single abstract type would be nice.Why this change is useful:
Uniontypes for zero-dimensional collections unnecessary: instead ofUnion{Number,Ref,AbstractArray{<:Any,0}}now it will be possible to write simply, and more generically,ShapefulIterator{0}. At least in some cases.ndimsandIteratorSizemethods:ndimsmethods that come with Julia is decreased by sixIteratorSizemethods that come with Julia is decreased by threeWhy this change doesn't prevent a better future supertype choice:
Number,AbstractCharandRefsubtype a parameterless abstract type likeZeroDimensionalIterator. However:ShapefulIterator{N}ShapefulIteratordoesn't preclude addingZeroDimensionalIterator <: ShapefulIterator{0}later.AbstractArray{T}subtype a different abstract type, with an element type type parameter, something likeAbstractArray{T,N} <: TypedIterator{T}. In this case, however,TypedIteratorwould be useless for the zero dimensional iterators mentioned above, as they are their own element types, something not expressible in abstract type subtyping.